<?php
	include_once "get_db.php";
	
	$function_name = $_REQUEST['function_name'];
	if($function_name == 'getSegmentDetailByID') {
		$node_array = array();
		$way_id = $_REQUEST['way_id'];
		$result = getSegmentDetailByID($way_id);
		while($data = mysql_fetch_array($result)) {
			$result_node = getNodeBydID($data["nd"]);
			$node = mysql_fetch_row($result_node);
			$custom_node['id'] = $node[0];
			$custom_node['lat'] = $node[1];
		    $custom_node['lng'] = $node[2];
			$node_array[] = $custom_node;
		}
		echo '{"way":'.json_encode($node_array).'}';
	} elseif($function_name == 'getSegment') {
		$node_array = array();
		$result = getSegment();
		while($data = mysql_fetch_array($result)) {
			$segment['id'] = $data['id'];
			$segment['start_node'] = $data['start_node'];
		    $segment['end_node'] = $data['end_node'];
			$node_array[] = $segment;
		}
		echo '{"segment":'.json_encode($node_array).'}';
	} elseif($function_name == 'getNodeBydID') {
		$node_id = $_REQUEST['node_id'];
		$result = getNodeBydID($node_id);
		$data = mysql_fetch_row($result);
		$node['lat'] = $data[1];
		$node['lng'] = $data[2];
		echo json_encode($node);
	} elseif($function_name == 'getSegmentByStartNode') {
		$start_node_id = $_REQUEST['start_node_id'];
		$result_node = getNodeBydID($start_node_id);
		$data_node = mysql_fetch_row($result_node);
		$node['lat'] = $data_node[1];
		$node['lng'] = $data_node[2];
		$result_segment = getSegmentByStartNode($start_node_id);
		$array_segment = array();
		while($data = mysql_fetch_array($result_segment)) {			
			$result_endnode = getNodeBydID($data['end_node']);
			$data_node = mysql_fetch_row($result_endnode);
			$node_temp['lat'] = $data_node[1];
			$node_temp['lng'] = $data_node[2];
			$segment['end_node'] = $node_temp;
			$array_segment[] = $segment;
		}
		echo '{"node":'.json_encode($node).'},'.'{"segment":'.json_encode($array_segment).'}';
	} elseif($function_name == 'getNode') {
		$return = array();
		$result = getNode();
		while($data = mysql_fetch_array($result)) {	
			$node['id'] = $data['id'];
			$return[] = $node;
		}		
		echo '{"node":'.json_encode($return).'}';
	}
?>